//
//  NewsViewController.m
//  项目一TimeMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 huangmingyu. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsModel.h"
#import "NewsViewCell.h"

@interface NewsViewController () {
    
    NSMutableArray *_arrayData;
    UIImageView *TitleimageView;
    UIPageControl *_pageControl;
}

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.newsTableView.dataSource = self;
    self.newsTableView.delegate = self;
    self.title = @"新闻";
    
    [self loadData];
    [self configUI];
    
}

- (void)loadData {
    
    _arrayData = [NSMutableArray array];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"news_list" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                     options:NSJSONReadingMutableContainers
                                                       error:nil];
    for (NSDictionary *dic in array) {
        NewsModel *model = [[NewsModel alloc] init];
        
        model.newsID = [dic[@"newsid"] integerValue];
        model.image = dic[@"image"];
        model.summaryLabel = dic[@"summary"];
        model.titleLabel = dic[@"title"];
        model.newsTpye = (NewsTpye)[dic[@"type"] integerValue];
        
        [_arrayData addObject:model];
        
        
        
        
    }
    
}

- (void)configUI {
    
    self.newsTableView.backgroundColor = [UIColor clearColor];
    self.newsTableView.separatorColor = [UIColor clearColor];
    self.newsTableView.rowHeight = 100;
    
    NewsModel *model = _arrayData[0];
    TitleimageView = [[UIImageView alloc] init];
    [TitleimageView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    TitleimageView.frame = CGRectMake(0, 64 , kScreenWidth, 150);
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    [_arrayData removeObjectAtIndex:0];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, kScreenWidth, 30)];
    label.text = model.titleLabel;
    label.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.4];
    label.font = [UIFont systemFontOfSize:20];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [view addSubview:label];
    
    self.newsTableView.tableHeaderView = view;
    view.backgroundColor = [UIColor clearColor];
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 150)];
    
    
    for (int i = 0; i < _arrayData.count; i++) {
        
        UIImageView *imageViews = [[UIImageView alloc] initWithFrame:view.frame];
        [imageViews sd_setImageWithURL:[NSURL URLWithString:model.image]];
        
        
    }
    [view insertSubview:scroll belowSubview:label];
    
    scroll.showsVerticalScrollIndicator = NO;
    scroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scroll.contentSize = CGSizeMake(kScreenWidth * _arrayData.count, 0);
    scroll.pagingEnabled = YES;
    
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    _pageControl.center = CGPointMake(kScreenWidth / 2, 184);
    _pageControl.numberOfPages = _arrayData.count;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPage = 0;
    
    [view insertSubview:_pageControl aboveSubview:scroll];
    scroll.delegate = self;
    
    
    
    [self.view insertSubview:TitleimageView belowSubview:self.newsTableView];
    
    
    
    //注册单元格
    [self.newsTableView registerNib:[UINib nibWithNibName:@"NewsViewCell" bundle:nil] forCellReuseIdentifier:@"123"];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat ofy = scrollView.contentOffset.y;
    CGFloat top = scrollView.contentInset.top;
    CGFloat y = ofy + top;
    NSLog(@"%f,%f", ofy,top);
    NSLog(@"%f", y);
    if (y < 0) { //向下滑动，图片放大
        //对imageframe修改，需要改哪些值？
        //        transform 中心点放大，不好用
        //        imageView.transform = CGAffineTransformMakeScale((150 - y) / 150, (150 - y) / 150);
        CGFloat width =  kScreenWidth * (150 - y) / 150;//屏幕宽度*放大倍率
        CGFloat height = 150 - y;
        TitleimageView.frame = CGRectMake((kScreenWidth - width) / 2, 64, width, height);
    }else // 向上滑动，图片推到屏幕上面
    {
        TitleimageView.frame = CGRectMake(0, -y + 64, kScreenWidth, 150);
    }
    
    _pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _arrayData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    
    cell.model = _arrayData[indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSString *typeWebViewName = nil;
    
    switch (cell.model.newsTpye) {
        case WordType:
            typeWebViewName = @"wenzi";
            break;
        case ImageType:
            typeWebViewName = @"tupian";
            break;
        case VideoType:
            typeWebViewName = @"shipin";
            break;
        default: return;
            break;
    }
    
    UIViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:typeWebViewName];
    [self.navigationController pushViewController:viewCtrl animated:YES];
    
    viewCtrl.hidesBottomBarWhenPushed = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
